﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using iTunesLib;
using System.IO;
using System.Threading;

namespace AlbunTest
{
    public partial class Form1 : Form
    {
        private delegate void ShowProccessDelegete(int max, int curr, string msg);
        private delegate void ExcuteTaskDelegete();
        private void ShowProccess(int max, int curr, string msg)
        {
            if (progressBar1.InvokeRequired)
            {
                ShowProccessDelegete proccess = new ShowProccessDelegete(ShowProccess);
                this.BeginInvoke(proccess, new object[] { max, curr, msg });
            }
            else
            {
                this.progressBar1.Maximum = max;
                this.progressBar1.Value = curr;
                this.label1.Text = msg;
                if (curr == max)
                {
                    this.label1.Text = "处理完成";
                }
            }
        }

        private void ExcuteTask()
        {
            ShowProccessDelegete process = new ShowProccessDelegete(ShowProccess);
            #region Itunes
            MusicPlayEntities context1 = new MusicPlayEntities();            
            iTunesAppClass itunes = new iTunesAppClass();            
            int j = 1;
            string albumName = "";
            string imgPath = "";
            var plCount=itunes.LibrarySource.Playlists.Count;
            int totalTracks = itunes.LibrarySource.Playlists[plCount].Tracks.Count;
            var obj = itunes.LibrarySource.Playlists[0];
            foreach (IITTrack tr in itunes.LibrarySource.Playlists[plCount].Tracks)
            {
                try
                {

                    AACInfo acc = new AACInfo();
                    acc.Album = tr.Album;
                    acc.AlbumArtist = ((dynamic)tr).AlbumArtist;
                    acc.Artist = tr.Artist;
                    acc.BitRate = tr.BitRate;
                    acc.Duration = tr.Duration;
                    acc.Genre = tr.Genre;

                    acc.Lyrics = ((dynamic)tr).Lyrics;
                    acc.Name = tr.Name;
                    acc.PlayedCount = tr.PlayedCount;
                    acc.ReleaseDate = ((dynamic)tr).ReleaseDate;
                    acc.Size = tr.Size;
                    acc.Time = tr.Time;
                    acc.TrackNumber = tr.TrackNumber;
                    acc.Year = tr.Year;
                  
                    //if (albumName != tr.Album)
                    //{
                    //    imgPath = Guid.NewGuid().ToString()+".jpg";

                    //    tr.Artwork[1].SaveArtworkToFile(@"D:\BaiDuCloud\百度云\Music\AlbumImg\" + imgPath);                       
                    //    albumName = tr.Album;
                    //}
                    //acc.IMGPath = imgPath;
                    //string fileName = Guid.NewGuid().ToString() + ".m4a";
                    //File.Copy(((dynamic)tr).Location, @"D:\BaiDuCloud\百度云\Music\Songs\" + fileName);
                    //acc.Location = fileName;
                    //context1.AACInfoes.AddObject(acc);
                    //context1.SaveChanges();
                    ShowProccess(totalTracks, j, "正在处理:" + acc.Name);                   
                    j++;
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
            #endregion
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExcuteTaskDelegete exec = new ExcuteTaskDelegete(ExcuteTask);
            exec.BeginInvoke(null, null);
        }
    }
}
